import Vue from "vue";
import store from "./store";
import Router from "vue-router";
import Home from "./views/Home.vue";
const Logpage = () => import("./views/log_page.vue");
const articlePage = () => import("./views/article_page.vue");
const articleTuiJian = () => import("./components/shouYe/article_tuijian.vue");
const articleGuanZhu = () => import("./components/shouYe/article_guanzhu.vue");
const articleSheHui = () => import("./components/shouYe/article_shehui.vue");
const articleJiaTing = () => import("./components/shouYe/article_jiating.vue");
const articleRenWen = () => import("./components/shouYe/article_renwen.vue");
const articleGanQing = () => import("./components/shouYe/article_ganqing.vue");
const articleLiShi = () => import("./components/shouYe/article_lishi.vue");
const articleLogin = () => import("./components/shouYe/article_login.vue");

Vue.use(Router, store);

export default new Router({
  mode: "history",
  base: process.env.BASE_URL,
  routes: [
    {
      path: "/",
      // name: "home",
      component: Home,
      meta: {
        keepAlive: true // 需要被缓存
      },
      // beforeEnter: (to, from, next) => {
      //   if (store.state.isLogin == 1) {
      //     console.log("用户已经登录");
      //     next();
      //   } else {
      //     console.log("用户未登录");
      //     next({ path: "/", query: { Rurl: to.fullPath } }); //未登录则跳转到登陆界面，query:{ Rurl: to.fullPath}表示把当前路由信息传递过去方便登录后跳转回来
      //   }
      // }
      children: [
        {
          name: "articleTuiJian",
          path: "",
          component: articleTuiJian,
          meta: {
            keepAlive: true, // 需要被缓存
            requireAuth: true
          },
          beforeEnter: (to, from, next) => {
            store.commit("setPageNum", 1);
            next();
          }
        },
        {
          name: "articleGuanZhu",
          path: "GuanZhu",
          component: articleGuanZhu,
          meta: {
            keepAlive: true // 需要被缓存
          },
          beforeEnter: (to, from, next) => {
            store.commit("setPageNum", 0);
            if (store.state.isLogin == 1) {
              next();
            } else {
              next({
                name: "articleLogin",
                query: { redirect: to.fullPath } // 将跳转的路由path作为参数，登录成功后跳转到该路由
              });
            }
          }
        },
        {
          name: "articleSheHui",
          path: "SheHui",
          component: articleSheHui,
          meta: {
            keepAlive: true // 需要被缓存
          },
          beforeEnter: (to, from, next) => {
            store.commit("setPageNum", 2);
            next();
          }
        },
        {
          name: "articleJiaTing",
          path: "JiaTing",
          component: articleJiaTing,
          meta: {
            keepAlive: true // 需要被缓存
          },
          beforeEnter: (to, from, next) => {
            store.commit("setPageNum", 3);
            next();
          }
        },
        {
          name: "articleRenWen",
          path: "RenWen",
          component: articleRenWen,
          meta: {
            keepAlive: true // 需要被缓存
          },
          beforeEnter: (to, from, next) => {
            store.commit("setPageNum", 4);
            next();
          }
        },
        {
          name: "articleGanQing",
          path: "GanQing",
          component: articleGanQing,
          meta: {
            keepAlive: true // 需要被缓存
          },
          beforeEnter: (to, from, next) => {
            store.commit("setPageNum", 5);
            next();
          }
        },
        {
          name: "articleLiShi",
          path: "LiShi",
          component: articleLiShi,
          meta: {
            keepAlive: true // 需要被缓存
          },
          beforeEnter: (to, from, next) => {
            store.commit("setPageNum", 6);
            next();
          }
        },
        {
          name: "articleLogin",
          path: "articleLogin/:`fromQuery`",
          component: articleLogin
        }
      ]
    },
    {
      path: "/about",
      name: "about",
      // route level code-splitting
      // this generates a separate chunk (about.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () =>
        import(/* webpackChunkName: "about" */ "./views/About.vue")
    },
    {
      path: "/logPage",
      name: "logPage",
      component: Logpage
    },
    {
      path: "/articlePage/:articleDetail",
      name: "articlePage",
      component: articlePage,
      meta: {
        keepAlive: false // 不需要被缓存
      }
    }
  ]
});
